import apis from "./homeApi";

// const datasource = {
//   dsCount: "126560",
//   latestDsName: "数据源名称",
//   latestDsSubmitTime: "2019-03-29 12:21:21"
// };

// const otherCount = {
//   sendTotalCount: 8846,
//   sendTodayCount: 1234,
//   readTotalCount: 6560,
//   commentTotalCount: 2978
// };

export default {
  namespace: "home",
  state: {
    datasource: {},
    otherCount: {},
    pushDepList: [], // 推送部门
    pushData: [], // 柱状图
    rankList: []
  },
  effects: {
    // 数量卡片
    *fetchTop4Count(_, { call, put }) {
      const res = yield call(apis.fetchTop4Count || {});
      if (res[0].errCode === 0) {
        yield put({
          type: "updateState",
          payload: { datasource: res[0].datas }
        });
      }
      if (res[1].errCode === 0) {
        yield put({
          type: "updateState",
          payload: { otherCount: res[1].datas || {} }
        });
      }
    },

    // 推送统计
    *fetchPushCount({ payload }, { call, put }) {
      const { action, params } = payload;
      const { errCode, datas = [] } = yield call(apis[action], params);
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { pushData: datas || [] }
        });
      }
    },

    *fetchDepList({ payload }, { call, put }) {
      const { errCode, datas } = yield call(apis.fetchDepList, payload);
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { pushDepList: datas || [] }
        });
      }
    },
    *fetchSceneRank({ payload }, { call, put, select }) {
      const { errCode, datas } = yield call(apis.fetchSceneRank, payload);
      if (errCode === 0) {
        const { rankList = [] } = yield select(state => state.home);
        rankList[0] = datas || [];
        yield put({
          type: "updateState",
          payload: { rankList }
        });
      }
    },
    *fetchPlanRank({ payload }, { call, put, select }) {
      const { errCode, datas } = yield call(apis.fetchPlanRank, payload);
      if (errCode === 0) {
        const { rankList = [] } = yield select(state => state.home);
        rankList[1] = datas || [];
        yield put({
          type: "updateState",
          payload: { rankList }
        });
      }
    }
  },
  reducers: {
    updateState: (state, { payload }) => ({
      ...state,
      ...payload
    })
  }
};
